var ns, toast, dialog, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        IsReject: 0,//0 验收  1 退回
        ID: 0,
        ReleaseExplain: '', //信息
        placeholderText: '请输入验收信息(选填)',
        imglist: [],
        // imglist:[
        //     {ID:0,url:'https://test.saasyy.com/upload/WechatServiceImg/20220324/44d7f534f7e149e9811fb64c2d18e719_20090o000000fddkaECCF_D_769_510_Q100.jpg'},
        //     {ID:1,url:'https://test.saasyy.com/upload/WechatServiceImg/20220218/752a16682505453cb0f6f8f00c926987_头像.jpg'},
        //     {ID:2,url:'https://test.saasyy.com/upload/WechatServiceImg/20220211/8d307ac2fc074381b9db0f54a7ce0601_picture_f5303b3.jpg'},
        // ],
        maxuploadcount: 4,
    },
    methods: {
        do_save: function () {
            var that = this;
            var options = {};
            if (that.imglist.length <= 0) {
                api.toast({
                    msg: '请上传至少一张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }

            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            options.id = that.ID;
            options.remark = that.ReleaseExplain;
            options.action = "checkdelivery";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '操作成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                setTimeout(function () {
                    //刷新
                    api.sendEvent({
                        name: 'do_load_data',
                    });
                    api.closeWin();
                }, 500);
            }, {
                files: {
                    file: filelist
                }
            });
        },
        choose_picture: function () {
            var that = this;
            ns.confirmPer('camera', function () {
                that.choose_picture_start();
            })
        },
        choose_picture_start: function () {
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            UIAlbumBrowser.imagePicker({
                max: that.maxuploadcount - that.imglist.length,
                styles: {
                    bg: '#000000',
                    mark: {
                        position: 'top_right',
                        size: 20
                    },
                    nav: {
                        bg: '#000000',
                        cancelColor: '#fff',
                        cancelSize: 16,
                        nextStepColor: '#7fff00',
                        nextStepSize: 16
                    },
                    thumbnail: { //（可选项）返回的缩略图配置，**建议本图片不要设置过大** 若已有缩略图，则使用已有的缩略图。若要重新生成缩略图，可先调用清除缓存接口api.clearCache()。
                        w: 100, //（可选项）数字类型；返回的缩略图的宽；默认：原图的宽度
                        h: 100 //（可选项）数字类型；返回的缩略图的宽；默认：原图的高度
                    }
                },
                animation: true,
            }, function (ret) {
                if (ret.eventType == 'nextStep') {
                    if (ret.list && ret.list.length > 0) {
                        that.select_imglist = ret.list;
                        that.select_imgindex = 0;
                        that.UIAlbumBrowser_transPath();
                    }
                    UIAlbumBrowser.closePicker();
                }
                if (ret.originalPath && ret.originalPath.length > 0) {
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.originalPath,
                        thumbPath: ret.originalPath
                    });
                    that.imgindex++;
                }
            });
            return;
        },
        UIAlbumBrowser_transPath: function () {
            var that = this;
            var item = that.select_imglist[that.select_imgindex];
            UIAlbumBrowser.transPath({
                path: item.path
            }, function (ret, err) {
                if (ret) {
                    that.select_imgindex++;
                    if (that.select_imgindex < that.select_imglist.length) {
                        that.UIAlbumBrowser_transPath();
                    } else {
                        that.select_imgindex = 0;
                    }
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.path,
                        thumbPath: item.thumbPath
                    });
                    that.imgindex++;
                }
            });
        },

        remove_img: function (index) {
            var that = this;
            for (var i = 0; i < that.imglist.length; i++) {
                var item = that.imglist[i];
                if (item.index == index) {
                    that.imglist.splice(i, 1);
                }
            }

        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();
    UIAlbumBrowser = api.require('UIAlbumBrowser');

    app.ID = api.pageParam.ID;
    app.IsReject = api.pageParam.IsReject;
    if (app.IsReject == 1) {
        app.placeholderText = '请输入退回原由(选填)';
    }
};
